package com.app.twth.ui.b2btrailer.submit;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseCameraPhotoActivity;
import com.app.twth.bean.FilePathListDTO;
import com.app.twth.bean.PhotoBean;
import com.app.twth.databinding.ActivityB2bTrailerOblpnSubmitBinding;
import com.app.twth.ui.b2btrailer.B2BTrailerViewModel;
import com.app.twth.ui.checkin.PhotoAdapter;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @describe :
 */
public class B2BTrailerSubmitActivity extends BaseCameraPhotoActivity<ActivityB2bTrailerOblpnSubmitBinding, B2BTrailerViewModel> {
    private String TAG = getClass().getSimpleName();

    private int type = 0;
    private int position = 0;
    private List<PhotoBean> bolPaths = new ArrayList<>();
    private List<PhotoBean> goodsPaths = new ArrayList<>();
    private List<PhotoBean> sealPaths = new ArrayList<>();
    private List<PhotoBean> afterPaths = new ArrayList<>();
    private PhotoAdapter bolAdapter, goodsAdapter, sealAdapter, afterAdapter;

    @Override
    protected void initListener() {
        viewModel.fileUploadMutableLiveData.observe(this, data -> {
            Log.e(TAG, "type():" + data.getType() + ",position:" + data.getPosition());
            switch (data.getType()) {
                case 0:
                    if (bolPaths.size() > data.getPosition()) {
                        PhotoBean photoBean = bolPaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
                case 1:
                    if (goodsPaths.size() > data.getPosition()) {
                        PhotoBean photoBean = goodsPaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
                case 2:
                    if (sealPaths.size() > data.getPosition()) {
                        PhotoBean photoBean = sealPaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
                case 3:
                    if (afterPaths.size() > data.getPosition()) {
                        PhotoBean photoBean = afterPaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
            }

            if (data.isSubmit()) {
                submitData();
            }
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.submitLiveData.observe(this, data -> {

            Bundle bundle = new Bundle();
            if (data == null || data.size() == 0) {
                bundle.putBoolean("isFilish", true);
            } else {
                bundle.putBoolean("isFilish", false);
            }
            Intent intent = new Intent();
            intent.putExtra(AppRouter.K_EXTRA, bundle);
            setResult(RESULT_OK, intent);
            finish();
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "B2BTrailer(Submit)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setReset(getString(R.string.submit));

        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        viewModel.b2bTrailerQrLiveData.setValue((B2bTrailerQr) bundle.getSerializable("B2bTrailerQr"));

        binding.tvTrailer.setText("(" + viewModel.b2bTrailerQrLiveData.getValue().getRefId() + ")" + viewModel.b2bTrailerQrLiveData.getValue().getOrderNo());

        bolPaths.add(new PhotoBean());
        bolAdapter = new PhotoAdapter(this, bolPaths, 6, position -> {
            type = 0;
            this.position = position;
            requestCamera("bol");
        }, str -> {
            type = 0;
        });
        GridLayoutManager bolManager = new GridLayoutManager(this, 3);
        binding.rvBol.setLayoutManager(bolManager);
        binding.rvBol.setAdapter(bolAdapter);

        goodsPaths.add(new PhotoBean());
        goodsAdapter = new PhotoAdapter(this, goodsPaths, 1, position -> {
            type = 1;
            this.position = position;
            requestCamera("goods");
        }, str -> {
            type = 1;
        });
        GridLayoutManager goodsManager = new GridLayoutManager(this, 3);
        binding.rvGoods.setLayoutManager(goodsManager);
        binding.rvGoods.setAdapter(goodsAdapter);

        sealPaths.add(new PhotoBean());
        sealAdapter = new PhotoAdapter(this, sealPaths, 1, position -> {
            type = 2;
            this.position = position;
            requestCamera("seal");
        }, str -> {
            type = 2;
        });
        GridLayoutManager sealManager = new GridLayoutManager(this, 3);
        binding.rvSeal.setLayoutManager(sealManager);
        binding.rvSeal.setAdapter(sealAdapter);

        afterPaths.add(new PhotoBean());
        afterAdapter = new PhotoAdapter(this, afterPaths, 1, position -> {
            type = 3;
            this.position = position;
            requestCamera("after");
        }, str -> {
            type = 3;
        });
        GridLayoutManager afterManager = new GridLayoutManager(this, 3);
        binding.rvAfter.setLayoutManager(afterManager);
        binding.rvAfter.setAdapter(afterAdapter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_reset:
                submitData();
                break;
        }
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void getImgPath(String path) {
        setPath(type, path);
    }

    private void setPath(int type, String pathImage) {
        Log.e(TAG, "setPath>type:" + type + ",pathImage:" + pathImage);

        if (!pathImage.contains("http")) {//选择的图片
            String fileName = "";
            switch (type) {
                case 0:
                    fileName = "bol";
                    break;
                case 1:
                    fileName = "goods";
                    break;
                case 2:
                    fileName = "seal";
                    break;
                case 3:
                    fileName = "after";
                    break;
            }
            Log.e(TAG, "setPath>type:" + type + ",fileName:" + fileName);
            viewModel.fileUpload(viewModel.b2bTrailerQrLiveData.getValue().getOrderNo(), fileName, pathImage, type, 0, false, false);
        }

        PhotoBean imgUrl = new PhotoBean();
        imgUrl.setPathImg(pathImage);
        imgUrl.setUrlImg(pathImage);
        switch (type) {
            case 0:
                if (bolPaths.size() > position) {
                    bolPaths.remove(position);
                    bolPaths.add(position, imgUrl);
                }
                if (bolPaths.size() < 1) {
                    if (!Utils.isEmpty(bolPaths.get(bolPaths.size() - 1).getPathImg())) {
                        goodsPaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : bolPaths) {
                    Log.e(TAG, img.toString());
                }
                if (bolAdapter != null) {
                    bolAdapter.setData(bolPaths);
                }
                break;
            case 1:
                if (goodsPaths.size() > position) {
                    goodsPaths.remove(position);
                    goodsPaths.add(position, imgUrl);
                }
                if (goodsPaths.size() < 1) {
                    if (!Utils.isEmpty(goodsPaths.get(goodsPaths.size() - 1).getPathImg())) {
                        goodsPaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : goodsPaths) {
                    Log.e(TAG, img.toString());
                }
                if (goodsAdapter != null) {
                    goodsAdapter.setData(goodsPaths);
                }
                break;
            case 2:
                if (sealPaths.size() > position) {
                    sealPaths.remove(position);
                    sealPaths.add(position, imgUrl);
                }
                if (sealPaths.size() < 1) {
                    if (!Utils.isEmpty(sealPaths.get(sealPaths.size() - 1).getPathImg())) {
                        sealPaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : sealPaths) {
                    Log.e(TAG, img.toString());
                }
                if (sealAdapter != null) {
                    sealAdapter.setData(sealPaths);
                }
                break;
            case 3:
                if (afterPaths.size() > position) {
                    afterPaths.remove(position);
                    afterPaths.add(position, imgUrl);
                }
                if (afterPaths.size() < 1) {
                    if (!Utils.isEmpty(afterPaths.get(afterPaths.size() - 1).getPathImg())) {
                        afterPaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : afterPaths) {
                    Log.e(TAG, img.toString());
                }
                if (afterAdapter != null) {
                    afterAdapter.setData(afterPaths);
                }
                break;
        }
    }

    private void submitData() {
        List<String> sealPathList = new ArrayList<>();
        if (sealPaths.size() > 0) {
            for (int i = 0; i < sealPaths.size(); i++) {
                PhotoBean photoBean = sealPaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(viewModel.b2bTrailerQrLiveData.getValue().getOrderNo(), "seal", s, 2, position, true, true);
                        return;
                    }
                    sealPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(viewModel.b2bTrailerQrLiveData.getValue().getOrderNo(), "seal", photoBean.getPathImg(), 2, position, true, true);
                    return;
                }
            }
        }
        if (sealPathList.size() == 0) {
            ToastUtil.showToast("Please add seal photos");
            return;
        }

        List<String> goodsPathList = new ArrayList<>();
        if (goodsPaths.size() > 0) {
            for (int i = 0; i < goodsPaths.size(); i++) {
                PhotoBean photoBean = goodsPaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(viewModel.b2bTrailerQrLiveData.getValue().getOrderNo(), "goods", s, 1, position, true, true);
                        return;
                    }
                    goodsPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(viewModel.b2bTrailerQrLiveData.getValue().getOrderNo(), "goods", photoBean.getPathImg(), 1, position, true, true);
                    return;
                }
            }
        }
        if (goodsPathList.size() == 0) {
            ToastUtil.showToast("Please add goods photos");
            return;
        }

        List<String> afterPathList = new ArrayList<>();
        if (afterPaths.size() > 0) {
            for (int i = 0; i < afterPaths.size(); i++) {
                PhotoBean photoBean = afterPaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(viewModel.b2bTrailerQrLiveData.getValue().getOrderNo(), "after", s, 3, position, true, true);
                        return;
                    }
                    afterPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(viewModel.b2bTrailerQrLiveData.getValue().getOrderNo(), "after", photoBean.getPathImg(), 3, position, true, true);
                    return;
                }
            }
        }
        if (afterPathList.size() == 0) {
            ToastUtil.showToast("Please add after loading photos");
            return;
        }

        List<String> bolPathList = new ArrayList<>();
        if (bolPaths.size() > 0) {
            for (int i = 0; i < bolPaths.size(); i++) {
                PhotoBean photoBean = bolPaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(viewModel.b2bTrailerQrLiveData.getValue().getOrderNo(), "bol", s, 0, position, true, true);
                        return;
                    }
                    bolPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(viewModel.b2bTrailerQrLiveData.getValue().getOrderNo(), "bol", photoBean.getPathImg(), 0, position, true, true);
                    return;
                }
            }
        }
        if (bolPathList.size() == 0) {
            ToastUtil.showToast("Please add bol photos");
            return;
        }


        List<FilePathListDTO> filePathList = new ArrayList<>();

        FilePathListDTO bolFilePathListDTO = new FilePathListDTO();
        bolFilePathListDTO.setName("bol");
        bolFilePathListDTO.setPathList(bolPathList);
        filePathList.add(bolFilePathListDTO);

        FilePathListDTO goodsFilePathListDTO = new FilePathListDTO();
        goodsFilePathListDTO.setName("goods");
        goodsFilePathListDTO.setPathList(goodsPathList);
        filePathList.add(goodsFilePathListDTO);

        FilePathListDTO sealFilePathListDTO = new FilePathListDTO();
        sealFilePathListDTO.setName("seal");
        sealFilePathListDTO.setPathList(sealPathList);
        filePathList.add(sealFilePathListDTO);

        FilePathListDTO afterFilePathListDTO = new FilePathListDTO();
        afterFilePathListDTO.setName("after");
        afterFilePathListDTO.setPathList(afterPathList);
        filePathList.add(afterFilePathListDTO);

        viewModel.b2bTrailerQrLiveData.getValue().setFilePathList(filePathList);

        viewModel.b2bTrailerSubmit();
    }

}
